package toy.keli.edic.ui.main.components

import android.view.LayoutInflater
import android.view.View
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Button
import androidx.compose.material.Card
import androidx.compose.material.MaterialTheme
import androidx.compose.material.TextButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clipToBounds
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import org.intellij.lang.annotations.JdkConstants.HorizontalAlignment
import toy.keli.edic.R


class AlertDialogData {
    var okText = "确认"
    var body: String=""
    var title: String="title"
    var layout:Int=0
    var onLayoutInit:View.()->Unit ={}
    @Composable
    fun textBody(){
        if(layout!=0){
            AndroidView(factory = {
                //加载AndroidView布局。
                LayoutInflater.from(it).inflate(layout, null)
            }, modifier = Modifier
            ){
                onLayoutInit.invoke(it)
            }
        }else {
            Text(body)
        }
    }
}
fun AlertDialog(init:AlertDialogData.()->Unit,content: @Composable () -> Unit={}){
    InfoDialog.show {
        val data = AlertDialogData()
        init.invoke(data)
        androidx.compose.material.AlertDialog(
            backgroundColor = androidx.compose.material3.MaterialTheme.colorScheme.background,
            title = { Text(data.title) },
            text = {
                Column {
                    data.textBody()
                    content()
                }
            },
            onDismissRequest = {
//                InfoDialog.close()
            },
            confirmButton = {
                TextButton({
                    InfoDialog.close()
                }) { Text(data.okText) }
            },
            modifier = Modifier
        )
//        alert(data)
    }
}

@Composable
@Preview(showBackground = true)
fun alert(data:AlertDialogData=AlertDialogData()){
    Card {
        Column(modifier = Modifier.fillMaxSize()) {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .background(Color.LightGray)
                    .padding(10.dp)
            ) {
                Text(data.title)
            }
            Row {
                data.textBody()
            }
            Spacer(Modifier.weight(1f))
            Row {
                Spacer(Modifier.weight(1f))
                Button({
                    InfoDialog.close()
                }, modifier = Modifier) { Text(data.okText) }
                Spacer(Modifier.width(20.dp))
            }
        }
    }
}